//
//  MainViewController.swift
//  夏祖强的新浪微博
//
//  Created by 夏祖强 on 16/5/10.
//  Copyright © 2016年 夏祖强. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //在 ViewDidLoad 只是添加子控制器,不会创建tabBarButton
        //tabBar 的按钮们会在ViewWillApper (将要显示)方法之前创建
        
        addChildViewControllers()
        
        setupTabbar()
        
//        print(tabBar.subviews)
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
//        print(tabBar.subviews)
        
        //将按钮设置到顶部
        tabBar.bringSubviewToFront(composeButton)
        
        //计算按钮宽度
        let width = tabBar.bounds.width /  CGFloat(childViewControllers.count) - 1
        
        composeButton.frame = CGRectInset(tabBar.bounds, width*2, 0)
    }

    //MARK :-私有控件
    private lazy var composeButton:UIButton = {
    
        let button = UIButton()
        
        let imageName = "tabbar_compose_icon_add"
        
        button.setImage(UIImage(named:imageName), forState: .Normal)
        button.setImage(UIImage(named:imageName+"_highlighted"), forState: .Highlighted)
        let backImageName = "tabbar_compose_button"
        button.setBackgroundImage(UIImage(named:backImageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named:backImageName+"_highlighted"), forState: .Highlighted)
        
        //让按钮根据背景图片调整大笑
        button.sizeToFit()
        
        return button
    }()
}

//MARK -设置tabbar
extension MainViewController{
    
    //设置 tabbar
    private func setupTabbar(){
        //设置tabbar背景图片,一下两句必须连用, Nav 的处理类似
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 1.0,alpha: 1.0))
        tabBar.backgroundImage = UIImage(named:"tabbar_background")
        
        //添加撰写按钮
        tabBar.addSubview(composeButton)
    }
    
    
}
//通过extension 把相关的代码分组放置
extension MainViewController{
    
    private func addChildViewControllers(){
        
        addChildViewController(HomeViewController(),title: "首页",imageName: "tabbar_home");
        addChildViewController(MessageViewController(),title: "消息",imageName: "tabbar_message_center");
        //添加一个空白的控制器
        addChildViewController(UIViewController())

        
        addChildViewController(DiscoverViewController(),title: "发现",imageName: "tabbar_discover");
        addChildViewController(ProfileViewController(),title: "我",imageName: "tabbar_profile");
    }

    private func addChildViewController(vc: UIViewController,title:String,imageName:String){
        
//        let vc = HomeViewController()
        
        vc.title = title
        //设置文本属性
        //文本属性的 key 都包含AttributeName
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        //如果设置文字大小,需要设置Normal
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12)], forState: .Normal)
        
        vc.tabBarItem.image = UIImage(named:imageName)
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController : vc)
        
        addChildViewController(nav)
        
    
    }
}